package com.pb.infra.oth.exception;

import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.util.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 异常通用处理
 *
 * @author liudebao
 * @date 2020/1/8
 */
@Slf4j
@ControllerAdvice
@ResponseBody
public class ExceptionController {

    /**
     * 处理异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(Exception e) {
        if (e instanceof NullPointerException) {
            log.error("空指针异常", e);
            return Result.error(Constant.EXCEPTION_CODE, "空指针异常");
        } else if (e instanceof ArrayIndexOutOfBoundsException) {
            log.error("数组越界异常", e);
            return Result.error(Constant.EXCEPTION_CODE, "数组越界异常");
        } else if (e instanceof ClassNotFoundException) {
            log.error("未找到类异常", e);
            return Result.error(Constant.EXCEPTION_CODE, "未找到类异常");
        } else if (e instanceof ClassCastException) {
            log.error("类型转换异常", e);
            return Result.error(Constant.EXCEPTION_CODE, "类型转换异常");
        } else if (e instanceof NumberFormatException) {
            log.error("数字转换格式异常", e);
            return Result.error(Constant.EXCEPTION_CODE, "数字转换格式异常");
        } else if (e instanceof FileNotFoundException) {
            log.error("文件找不到异常", e);
            return Result.error(Constant.EXCEPTION_CODE, "文件找不到异常");
        } else if (e instanceof IOException) {
            log.error("IO异常", e);
            return Result.error(Constant.EXCEPTION_CODE, "IO异常");
        } else if (e instanceof NoSuchMethodException) {
            log.error("未找到对应的方法", e);
            return Result.error(Constant.EXCEPTION_CODE, "未找到对应的方法");
        } else if (e instanceof NoSuchFieldException) {
            log.error("未找到对应的字段或属性", e);
            return Result.error(Constant.EXCEPTION_CODE, "未找到对应的字段或属性");
        } else if (e instanceof BadSqlGrammarException) {
            log.error("sql错误异常", e);
            return Result.error(Constant.EXCEPTION_CODE, "sql错误异常");
        } else if (e instanceof DuplicateKeyException) {
            log.error("违反唯一性约束异常", e);
            return Result.error(Constant.EXCEPTION_CODE, "违反唯一性约束异常");
        } else if (e instanceof BizException) {
            log.error("违反自定义异常", e);
            return Result.error(((BizException) e).getCode(), e.getMessage());
        } else if (e instanceof AppAuthException) {
            log.error("违反自定义异常", e);
            return Result.error(((AppAuthException) e).getCode(), e.getMessage());
        } else {
            log.error("其他异常", e);
            return Result.error(Constant.EXCEPTION_CODE, e.getMessage());
        }
    }

}
